package com.wnhz.work.yz.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.YuShouAllDetailssActivity;
import com.wnhz.work.yz.activity.YuShouGotoPayActivity;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.YuShouListBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.wnhz.work.yz.view.NoScrollListView;
import com.wnhz.work.yz.view.RoundImageView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2017/4/12.
 */
public class YuShouAdapter extends BaseRVAdapter {
    private List<YuShouListBean.InfoEntity> infos = new ArrayList<>();
    private Context context;

    /**
     * 构造器
     *
     * @param context 上下文
     * @param list    数据集合
     */
    public YuShouAdapter(Context context, @NonNull List list) {
        super(context, list);
        this.infos = list;
        this.context = context;
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_yushou_list;
    }

    @Override
    public void onBind(BaseViewHolder holder, final int position) {
        holder.setTextView(R.id.order_time, "日期：" + infos.get(position).getOrder_time());
        holder.setTextView(R.id.order_name, "登记人：" + infos.get(position).getUser_name());

        holder.setTextView(R.id.all_num, infos.get(position).getSum_nums() + "套");
        holder.setTextView(R.id.get_num, infos.get(position).getHas_nums() + "套");
        holder.setTextView(R.id.else_num, infos.get(position).getOther_nums() + "套");
        if ("0".equals(infos.get(position).getPay_status())) {
            holder.setTextView(R.id.tv_type, "未支付");
            holder.getView(R.id.goto_pay).setVisibility(View.VISIBLE);
        } else {
            holder.getView(R.id.goto_pay).setVisibility(View.GONE);
            holder.setTextView(R.id.tv_type, "" + infos.get(position).getPay_type());
        }

        holder.getView(R.id.goto_pay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Log.e("--点击去支付--", "onSuccess:= " + infos.get(position).getType() + "===支付类型 == " + infos.get(position).getType());
                getAllInfo(infos.get(position).getFirst_need_money(), infos.get(position).getBudget_no(), infos.get(position).getType());
            }
        });

        holder.setTextView(R.id.tv_project_code, "订单号：" + infos.get(position).getBudget_no());
        holder.setTextView(R.id.xiangmu_total, "定金：" + infos.get(position).getFirst_need_money());
        String pay_money = "已支付定金：<font color=\"#ff0000\">￥" + infos.get(position).getFirst_need_money() + "</font>";
        holder.getTextView(R.id.tv_goods_first_money).setText(Html.fromHtml(pay_money));
        holder.setTextView(R.id.tv_all_price, "合计：¥" + infos.get(position).getTotal_fee() + "");

        final List<YuShouListBean.InfoEntity.DataEntity> dataList = infos.get(position).getData();

        NoScrollListView my_listView = holder.getView(R.id.my_listView);

        my_listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return dataList == null ? 0 : dataList.size();
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(final int position2, View convertView, ViewGroup parent) {
                ProjectContentViewHolder viewHolder;
                if (convertView == null) {
                    viewHolder = new ProjectContentViewHolder();
                    convertView = LayoutInflater.from(context).inflate(R.layout.project_content_item2, null);
                    viewHolder.iv_img = (RoundImageView) convertView.findViewById(R.id.iv_img);
                    viewHolder.tv_goods_name = (TextView) convertView.findViewById(R.id.tv_goods_name);
                    viewHolder.tv_product = (TextView) convertView.findViewById(R.id.tv_product);
                    viewHolder.tv_goods_info = (TextView) convertView.findViewById(R.id.tv_goods_info);
                    viewHolder.tv_goods_type = (TextView) convertView.findViewById(R.id.tv_goods_type);
                    viewHolder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
                    viewHolder.tv_goods_number = (TextView) convertView.findViewById(R.id.tv_goods_number);
                    viewHolder.get_num = (TextView) convertView.findViewById(R.id.get_num);
                    viewHolder.ll_max_item = (LinearLayout) convertView.findViewById(R.id.ll_max_item);
                    viewHolder.max_ll = (LinearLayout) convertView.findViewById(R.id.max_ll);
                    convertView.setTag(viewHolder);
                } else {
                    viewHolder = (ProjectContentViewHolder) convertView.getTag();
                }
                if ("0".equals(dataList.get(position2).getHas_nums())) {
                    viewHolder.max_ll.setVisibility(View.GONE);
                } else {
                    viewHolder.max_ll.setVisibility(View.VISIBLE);
                }
                viewHolder.get_num.setText(dataList.get(position2).getHas_nums() + "套");

                x.image().bind(viewHolder.iv_img, dataList.get(position2).getBudget_package_img());
                viewHolder.tv_goods_name.setText(dataList.get(position2).getPackage_name());
                viewHolder.tv_product.setText(dataList.get(position2).getCate_name());
                viewHolder.tv_goods_info.setText(dataList.get(position2).getSku_info());

                viewHolder.tv_price.setText("¥ " + dataList.get(position2).getPackage_money());

                viewHolder.tv_goods_number.setText("x" + dataList.get(position2).getNums());
                viewHolder.ll_max_item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        context.startActivity(new Intent(context,
                                YuShouAllDetailssActivity.class)
                                .putExtra("isisisyushou", 12345)
                                .putExtra("sku_id", dataList.get(position2).getSku_id())
                                .putExtra("goods_id", dataList.get(position2).getGoods_id())
                                .putExtra("budget_id", dataList.get(position2).getBudget_package_id())
                                .putExtra("type", infos.get(position).getType())
                                .putExtra("status", infos.get(position).getPay_status())
                                .putExtra("num", infos.get(position).getOther_nums())
                                .putExtra("name", dataList.get(position2).getPackage_name()));
                    }
                });
                return convertView;
            }

            class ProjectContentViewHolder {
                RoundImageView iv_img;
                TextView tv_goods_name;
                TextView tv_product;
                TextView tv_goods_info;
                TextView tv_goods_type;
                TextView tv_price;
                TextView tv_goods_number;
                LinearLayout ll_max_item;
                TextView get_num;
                LinearLayout max_ll;
            }
        });
    }

    private void getAllInfo(final String pay_money, String budget_no, final String type) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("type", type);
        map.put("budget_no", budget_no);
        XUtil.Post(Url.BOOKING_PAY, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("--预售订单--", "onSuccess:= " + result + "===支付类型 == " + type);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        JSONObject infos = new JSONObject(info);

                        setStart(pay_money, infos.optString("budget_no")
                                , infos.optString("wallet"), type);
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        Toast.makeText(context, "" + info, Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void setStart(String pay_money, String budget_no, String wallet, String type) {
        context.startActivity(new Intent(context, YuShouGotoPayActivity.class)
                .putExtra("pay_money", pay_money)
                .putExtra("budget_no", budget_no)
                .putExtra("wallet", wallet)
                .putExtra("type", type));

    }
}
